package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportTaskReplyRpcService;
import com.hunttown.mes.rpc.domain.TransportTaskReplyDTO;
import com.hunttown.mes.rpc.domain.query.TransportTaskReplyDTOQuery;
import com.hunttown.mes.service.TransportTaskReplyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportTaskReplyConverts.convert;

/**
 * created by wangjunfu on 2020-05-20 17:34:43
 */
@Service
public class TransportTaskReplyRpcServiceImpl implements TransportTaskReplyRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportTaskReplyRpcServiceImpl.class);

    private final TransportTaskReplyService transportTaskReplyService;

    @Autowired
    public TransportTaskReplyRpcServiceImpl(TransportTaskReplyService transportTaskReplyService) {
        this.transportTaskReplyService = transportTaskReplyService;
    }

    @Override
    public TransportTaskReplyDTO insert(TransportTaskReplyDTO transportTaskReplyDTO) {
        try {
            return convert(transportTaskReplyService.insert(convert(transportTaskReplyDTO)));
        } catch (Exception e) {
            logger.error("TransportTaskReply create failed.", e);
            throw new RuntimeException("TransportTaskReply create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportTaskReplyDTO transportTaskReplyDTO) {
        try {
            return transportTaskReplyService.updateInfoById(convert(transportTaskReplyDTO));
        } catch (Exception e) {
            logger.error("TransportTaskReply update by id failed.", e);
            throw new RuntimeException("TransportTaskReply update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportTaskReplyDTOQuery transportTaskReplyDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportTaskReplyDTOQuery.getW_id() != null && transportTaskReplyDTOQuery.getW_id() > 0) {
                query.put("w_id", transportTaskReplyDTOQuery.getW_id());
            }

            if (transportTaskReplyDTOQuery.getIds() != null && !transportTaskReplyDTOQuery.getIds().equals("")) {
                query.put("ids", transportTaskReplyDTOQuery.getIds());
            }

            if (transportTaskReplyDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportTaskReplyDTOQuery.getStopFlag());
            }

            if (transportTaskReplyDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportTaskReplyDTOQuery.getDeleteFlag());
            }

            return transportTaskReplyService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportTaskReply update by query failed.", e);
            throw new RuntimeException("TransportTaskReply update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportTaskReplyDTO selectById(Integer id) {
        try {
            return convert(transportTaskReplyService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportTaskReply select by id failed.", e);
            throw new RuntimeException("TransportTaskReply select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportTaskReplyDTO selectObjByQuery(TransportTaskReplyDTOQuery transportTaskReplyDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(transportTaskReplyService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportTaskReply select obj by query failed.", e);
            throw new RuntimeException("TransportTaskReply select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportTaskReplyDTO> getListForPage(TransportTaskReplyDTOQuery transportTaskReplyDTOQuery) {
        Query query = Query.page(transportTaskReplyDTOQuery.getPn(), transportTaskReplyDTOQuery.getPageSize());

        if (transportTaskReplyDTOQuery.getId() != null && transportTaskReplyDTOQuery.getId() > 0) {
            query.put("id", transportTaskReplyDTOQuery.getId());
        }

        if (transportTaskReplyDTOQuery.getIds() != null && !transportTaskReplyDTOQuery.getIds().equals("")) {
            query.put("ids", transportTaskReplyDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportTaskReplyDTOQuery.getLikeName())) {
            query.put("likeName", transportTaskReplyDTOQuery.getLikeName());
        }

        if (transportTaskReplyDTOQuery.getTaskId() != null && transportTaskReplyDTOQuery.getTaskId() > 0) {
            query.put("taskId", transportTaskReplyDTOQuery.getTaskId());
        }

        if (StringUtils.isNotBlank(transportTaskReplyDTOQuery.getShowDate())) {
            query.put("showDate", transportTaskReplyDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportTaskReplyDTOQuery.getStartDate())) {
            query.put("startDate", transportTaskReplyDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportTaskReplyDTOQuery.getEndDate())) {
            query.put("endDate", transportTaskReplyDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportTaskReplyDTOQuery.getOrderBy())) {
            query.put("orderBy", transportTaskReplyDTOQuery.getOrderBy());
        }

        if (transportTaskReplyDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportTaskReplyDTOQuery.getStopFlag());
        }

        if (transportTaskReplyDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportTaskReplyDTOQuery.getDeleteFlag());
        }

        List<TransportTaskReplyDTO> list = convert(transportTaskReplyService.selectByQuery(query));
        Page<TransportTaskReplyDTO> page = Page.from(transportTaskReplyDTOQuery.getPn(), transportTaskReplyDTOQuery.getPageSize(), list);
        page.setTotalItems(transportTaskReplyService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportTaskReplyService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportTaskReply delete failed.", e);
            throw new RuntimeException("TransportTaskReply delete failed, Please order your code.", e);
        }
    }
}